package com.xjc.system.utils;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.xjc.business.entity.response.Response;
import lombok.Data;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 徐锦程
 * 时间:2025-02-03 16:28
 */
@Component
public class FaceCardMatchUtils {

    private static final ObjectMapper objectMapper = new ObjectMapper();

    /**
     * 人脸证件比对
     * @param idCard 身份证号
     * @param name 姓名
     * @param image 图片
     * @return resultCode
     * 返回值为 1001 : 身份证号与姓名匹配，人脸判断为同一人  0.45-1
     * 返回值为 1002 : 身份证号与姓名匹配，人脸不能确定为同一人 0.4-0.45
     * 返回值为 1003 : 身份证号与姓名匹配，人脸判断为不同人  0-0.4
     * 返回值为 1004 : 无法识别  无分数，身份证姓名不一致等
     */
    public static Integer match(String idCard, String name,String image) {
        String host = "https://kzfacev1.market.alicloudapi.com";
        String path = "/api-mall/api/face_id_card_yi_suo/check";
        String method = "POST";
        String appcode = "864fda52ac464a3ab4da3a1f18aa9b7e";
        Map<String, String> headers = new HashMap<String, String>();
        headers.put("Authorization", "APPCODE " + appcode);
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        Map<String, String> querys = new HashMap<String, String>();
        Map<String, String> bodys = new HashMap<String, String>();
        bodys.put("idcard", idCard);
        bodys.put("name",name);
        bodys.put("image",image);

        try {

            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
            Response responseObj = objectMapper.readValue(EntityUtils.toString(response.getEntity()), Response.class);
            if(responseObj.getData()==null){
                return responseObj.getCode();
            }
            return responseObj.getData().getResultCode();
        } catch (Exception e) {
            return 0;
        }
    }

}
